<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserFromPostRequest;

class UserControllers extends Controller
{
	public function getIndex(Request $request)
	{
		//查询所有的用户数据
		//$users = DB::table('user')->get();
        $num = $request->input('num',5);
        if($request->has('keywords')){
            $users = DB::table('user')->where('name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
           $users = DB::table('user')->paginate(5); 
        }
        $data = $request->all();
        return view('Admin.user.index',['users'=>$users,'data'=>$data]);
	}

    public function getAdd()
    {
    	return view('Admin.user.add');
    }

    /**
     * 后台用户添加处理
     * 
     * @return insert 用户添加数据
     */
    public function postInsert(UserFromPostRequest $request)
    {
    	//查看提交过来的数据
    	//dd($request->all());//获取所有的请求数据
    	//dd($request->input('password'));//提取某一个字段数据
    	//dd($request->input('password','a@qq.com'));//设置默认值
   		//只提取部分数据
   		//$data = $request->only(['name','password']);
   		//除了哪些不要之外
   		//$data = $request->except(['name','password']);
   		//数据验证  手动验证
   		//if($request->input('name') == ''){
    	//	$request->flash();//闪存所有数据
    	//	return back();
    	//}
   		//return back()->withInput();//回到上一次请求的同时.闪存数据
   		//laravel提供的数据验证
    	// $this->validate($request,[
    	// 	'name' => 'required|max:255',
    	// 	'password' => 'required',
    	// 	'email' => 'required',
    	// ],[
    	// 	'name.required' => '用户名不能为空',
    	// 	'username.max' => '用户名最大不能超过255个字符',
    	// 	'password.required' => '密码不能为空',
    	// 	'email.required' => '邮箱账号不能为空',
    	// ]);
    	//提取数据
    	$data = $request->except('_token');
    	//调用方法进行头像的上传操作
    	$data['pic'] = $this->upload($request);
    	//进行hash加密
    	$data['password'] = Hash::make($data['password']);
    	//将数据进行入库操作
    	$res = DB::table('user')->insertGetId($data);
    	if($res){
    		//重定向到 user/index
    		return redirect('admin/user/index')->with('success','用户添加成功');
    	}else{
    		//回到上个页面
    		return back()->with('error','用户添加失败啦');
    	}
    }
    
    public function postDel(Request $request)
    {   
        
        $id = $request->input('id');
        $res = DB::table('user')->where('id',$id)->delete();
        echo $res;
    }

    public function getEdit($id)
    {
        $users = DB::table('user')->where('id',$id)->first();
        return view('Admin.user.edit',['users'=>$users]);
    }

    public function postUpdate(Request $request)
    {
        $data = $request->except('_token');
        if($request->hasFile('pic')){
           //调用方法进行头像的上传操作
           $data['pic'] = $this->upload($request); 
        }
        $res = DB::table('user')->where('id',$data['id'])->update($data);
        if($res){
            return redirect('admin/user/index')->with('success','用户修改成功');
        }else{
            return back()->with('error','用户修改失败');
        }
    }

    public function upload($request)
    {   
        if ($request->hasfile('pic')) {
            $aa = $request->file('pic')->getClientMimeType();
            $type = array('image/png','image/gif','image/jpeg');
            if(!in_array($aa, $type)){
                die('请选择正确的文件类型');
            }
            $name = time()+rand(1,999999);
            $su = $request->file('pic')->getClientOriginalExtension();
            $request->file('pic')->move('./public/uploads',$name.'.'.$su);
            return '/public/uploads/'.$name.'.'.$su;
        }
    }
}
